<?php
class ControllerCommonColumnLeft extends Controller {
	public function index() {
		//$t1=microtime(1);
		$this->load->model('design/layout');
		$this->load->model('catalog/category');
		$this->load->model('catalog/product');
		$this->load->model('catalog/information');
		/**
		 * **********************获取商品分类***************
		 */
		//记录三级分类个数：array('二级分类个数'=>该二级分类的三级分类数)
		$children_num = array();
		//二三级分类总数
		$sub_num = 0;
		$this->data['categories'] = array();
		
		$this->data['categories'] = $this->cache->get('categorieHomeIndex');
		if(!$this->data['categories']){
			$categories = $this->model_catalog_category->getCategories(0);
			//echo microtime(1)-$t1."<br>";
			foreach ($categories as $category) {
				if ($category['top']) {
					$children_data = array();
					//二级分类
					$children = $this->model_catalog_category->getCategories($category['category_id']);
					//计数器
					$i = 0;
					foreach ($children as $child) {
						$child_sum = 0;
						$data = array( 
							'filter_category_id' => $child['category_id'], 
							'filter_sub_category' => true 
						);
						
						if ($this->config->get('config_product_count')) {
							$product_total = $this->model_catalog_product->getTotalProducts($data);
							
							$child['name'] .= ' (' . $product_total . ')';
						}
						$little_children = $this->model_catalog_category->getCategories($child['category_id']);
						$children_num[$i]['num'] = count($little_children) + 1;
						$children_num[$i]['cat'] = $child['category_id'];
						$i++;
						$sub_num += count($little_children) + 1;
						$little_child = array();
						if (is_array($little_children) && count($little_children) > 0) {
							// 						$child_sum+=count($little_children);
							foreach ($little_children as $v) {
								$little_child[] = array( 
									'name' => $v['name'], 
									'href' => $this->url->link('product/category', 'path=' . $child['category_id'] . '_' . $v['category_id']) 
								);
							}
						}
						
						$children_data[$child['category_id']] = array( 
							'name' => $child['name'], 
							'children' => $little_child, 
							'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']) 
						);
					}
					//计算换列逻辑，基准数为15
					$base = 15;
					//列数
					$line = ceil($sub_num / $base);
					//列数不大于4
					$line = $line > 4 ? 4 : $line;
					$line_num = ceil($sub_num / $line);
					$line = 1;
					//列内 总数
					$line_sum = 0;
					if (count($children_data) != 0) {
						foreach ($children_num as $k => $v) {
							$line_sum += $v['num'];
							if ($line_sum >= $line_num && $line_sum <= 1.5 * $line_num) {
								$children_data[$v['cat']]['break'] = 1;
								$line_sum = 0;
								$line++;
							}
							elseif ($line_num > 1.5 * $line_num) {
								$children_data[$children_num[$k - 1]['cat']]['break'] = 1;
								$line_sum = $v['num'];
								$line++;
							}
						}
						$last = end($children_data);
						if ($last['break'])
							$line--;
					}
					else {
						$line = 0;
					}
					
					// 				$max_size=ceil(count($children_data+$child_sum)/10);
					// 				$size=0;
					// 				foreach ($children_data as $k=>$v){
					

					// 				}
					// Level 1
					$this->data['categories'][] = array( 
						'name' => $category['name'], 
						'id' => $category['category_id'], 
						'children' => $children_data, 
						'column' => $category['column'] ? $category['column'] : 1, 
						'href' => $this->url->link('product/category', 'path=' . $category['category_id']), 
						'width' => $line * 210 
					);
				}
			}
			$this->cache->set('categorieHomeIndex',$this->data['categories']);
		}
		
		//echo microtime(1)-$t1."<br>";
		/**
		 * **********************获取商品分类结束***************
		 */
		/**
		 * **********************获取热销商品***************
		 */
		$this->load->model('tool/image');
		$on_sales = $this->model_catalog_product->getBestSellerProducts(2);
		$this->data['on_sales'] = $this->model_tool_image->resizeProductImage($on_sales, 'image', 75, 65);
		//echo microtime(1)-$t1."<br>";
		/**
		 * **********************获取热销商品结束***************
		 */
		
		/**
		* 	打印机墨盒搜索
		*/
		$this->data['inkjets']=$this->model_catalog_category->getCategories(111);
		
		$this->data['toners']=$this->model_catalog_category->getCategories(121);


		/**
		 * *************获取左侧配置信息*****************
		 */
		$this->load->model('web/index');
		$this->data['shop_reason'] = $this->model_web_index->getSetting(1);
		// 博客信息
		$this->data['blogs'] = $this->model_web_index->getBlog();
		//echo microtime(1)-$t1."<br>";
		if (isset($this->request->get['route'])) {
			$route = (string) $this->request->get['route'];
		}
		else {
			$route = 'common/home';
		}
		
		$layout_id = 0;
		
		if ($route == 'product/category' && isset($this->request->get['path'])) {
			$path = explode('_', (string) $this->request->get['path']);
			
			$layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));
		}
		
		if ($route == 'product/product' && isset($this->request->get['product_id'])) {
			$layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
		}
		
		if ($route == 'information/information' && isset($this->request->get['information_id'])) {
			$layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
		}
		
		if (!$layout_id) {
			$layout_id = $this->model_design_layout->getLayout($route);
		}
		
		if (!$layout_id) {
			$layout_id = $this->config->get('config_layout_id');
		}
		$module_data = array();
		
		$this->load->model('setting/extension');
		
		$extensions = $this->model_setting_extension->getExtensions('module');
		foreach ($extensions as $extension) {
			$modules = $this->config->get($extension['code'] . '_module');
			if ($modules) {
				foreach ($modules as $module) {
					if ($module['layout_id'] == $layout_id && $module['position'] == 'column_left' && $module['status']) {
						$module_data[] = array( 
							'code' => $extension['code'], 
							'setting' => $module, 
							'sort_order' => $module['sort_order'] 
						);
					}
				}
			}
		}
		$sort_order = array();
		foreach ($module_data as $key => $value) {
			$sort_order[$key] = $value['sort_order'];
		}
		array_multisort($sort_order, SORT_ASC, $module_data);
		
		$this->data['modules'] = array();
		foreach ($module_data as $module) {
			$module = $this->getChild('module/' . $module['code'], $module['setting']);
			
			if ($module) {
				$this->data['modules'][] = $module;
			}
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/column_left.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/column_left.tpl';
		}
		else {
			$this->template = 'default/template/common/column_left.tpl';
		}
		//echo microtime(1)-$t1;
		$this->render();
	}
}
?>